package org.security.localstore;

import java.util.Optional;


public class UserContext {
    private static final ThreadLocal<Object> THREAD_LOCAL_UACS_USER = new ThreadLocal<>();


    public static void setUser(Object user) {
        THREAD_LOCAL_UACS_USER.set(user);
    }

    public static void remove() {
        THREAD_LOCAL_UACS_USER.remove();
    }

    public static Object currentUser() {
        return THREAD_LOCAL_UACS_USER.get();
    }

    private static String anonymous(){
        return "{user:null}";
    }

    public static Object currentUserOrAnonymous() {
        return currentUserOptional().orElse(anonymous());
    }

    public static Optional<Object> currentUserOptional() {
        Object user = THREAD_LOCAL_UACS_USER.get();
        if (user != null) {
            return Optional.of(user);
        }
        return Optional.empty();
    }

}
